package com.myclover.db;

import com.myclover.db.bean.Person;
import com.myclover.db.service.DBService;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class DBDemoActivity extends Activity {
	
	private DBService dbService;
	private EditText name;
	private EditText age;
	private EditText amount;
	private RadioButton male;
	private RadioButton female;
	private EditText userid;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dbService = new DBService(DBDemoActivity.this);
        initViewInfo();
    }
	
	public void initViewInfo(){
		name = (EditText)findViewById(R.id.name);
		age = (EditText)findViewById(R.id.age);
		amount = (EditText)findViewById(R.id.amount);
		male = (RadioButton)findViewById(R.id.male);
		female = (RadioButton)findViewById(R.id.female);
		userid = (EditText)findViewById(R.id.userid);
		Button saveBtn = (Button)findViewById(R.id.saveBtn);
		Button queryBtn = (Button)findViewById(R.id.queryBtn);
		Button updateBtn = (Button)findViewById(R.id.updateBtn);
		Button deleteBtn = (Button)findViewById(R.id.deleteBtn);
		saveBtn.setOnClickListener(listener);
		queryBtn.setOnClickListener(listener);
		updateBtn.setOnClickListener(listener);
		deleteBtn.setOnClickListener(listener);
		male.setOnCheckedChangeListener(radioListener);
		female.setOnCheckedChangeListener(radioListener);
		male.setChecked(true);
	}
	
	private OnClickListener listener = new View.OnClickListener() {
		
		public void onClick(View v) {
			Button btn = (Button)v;
			switch (btn.getId()) {
			case R.id.saveBtn:
				Person p = new Person();
				p.setName(name.getText().toString());
				p.setAge(Integer.parseInt(age.getText().toString()));
				p.setAmount(Float.parseFloat(amount.getText().toString()));
				if(male.isChecked()){
					p.setSex("��");
				}else{
					p.setSex("Ů");
				}
				dbService.save(p);
				show(R.string.saveSuccess);
				break;
			case R.id.queryBtn:
				male.setChecked(true);
				age.setText(null);
				amount.setText(null);
				Person person = dbService.findByName(name.getText().toString());
				if(person != null){
					age.setText(String.valueOf(person.getAge()));
					amount.setText(String.valueOf(person.getAmount()));
					userid.setText(String.valueOf(person.getId()));
					if("��".equals(person.getSex()) || "male".equals(person.getSex())){
						male.setChecked(true);
						female.setChecked(false);
					}else{
						male.setChecked(false);
						female.setChecked(true);
					}
				}
				break;
			case R.id.updateBtn:
				Person up = dbService.findByName(name.getText().toString());
				if(up != null){
					up.setName(name.getText().toString());
					up.setAge(Integer.parseInt(age.getText().toString()));
					up.setAmount(Float.parseFloat(amount.getText().toString()));
					up.setId(Integer.parseInt(userid.getText().toString()));
					if(male.isChecked()){
						up.setSex("��");
					}else{
						up.setSex("Ů");
					}
					dbService.update(up);
					show(R.string.updateSuccess);
				}
				break;
			case R.id.deleteBtn:
				Person dp = dbService.findByName(name.getText().toString());
				if(dp != null){
					dbService.delete(dp.getId());
					show(R.string.deleteSuccess);
					male.setChecked(true);
					age.setText(null);
					amount.setText(null);
					name.setText(null);
				}
				break;
			}
		}
	};
	
	private OnCheckedChangeListener radioListener = new OnCheckedChangeListener() {
		
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			RadioButton rbtn = (RadioButton)buttonView;
			switch (rbtn.getId()) {
			case R.id.male:
				if(isChecked){
					male.setChecked(true);
					female.setChecked(false);
				}
				break;
			case R.id.female:
				if(isChecked){
					male.setChecked(false);
					female.setChecked(true);
				}
				break;
			}
		}
	};
	
	public final void show(int id){
		Toast.makeText(DBDemoActivity.this, id, Toast.LENGTH_LONG).show();
	}

	@Override
	protected void onDestroy() {
		if(dbService != null){
			dbService.closeDB();
		}
		super.onDestroy();
	}
	
	
}
